package com.android.shop.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.widget.Toast;

public class Utils {
	/*
	 * 判断是否是数字
	 */
	public static boolean isNumeric(String str) {
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNullOrEmpty(Object obj) {
		if (obj != null && !obj.equals("")) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测是否有可用网络
	 */
	public static boolean checkNetWork(final Context context, boolean isShow) {
		boolean flag = false;
		ConnectivityManager cwjManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cwjManager.getActiveNetworkInfo() != null)
			flag = cwjManager.getActiveNetworkInfo().isAvailable();
		if (!flag && isShow) {
			Toast.makeText(context, "网络出错了,请检测网络连接是否打开", Toast.LENGTH_SHORT)
					.show();
		}
		return flag;
	}
	
	/**
	 * 将Bitmap转换成InputStream 
	 */
    public static InputStream Bitmap2InputStream(Bitmap bm) {  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
        InputStream is = new ByteArrayInputStream(baos.toByteArray());  
        return is;  
    }
}
